import java.util.List;

public class MySingleList implements IList {
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    @Override
    //链表头插
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }
    @Override
    //链表尾插
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
        }else {
            ListNode cur = head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }

    }
    @Override
    //求当前列表是否存在key
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    @Override
    //求当前列表有多少个节点
    public int size() {
        int size = 0;
       ListNode cur = head;
       while (cur != null) {
           size++;
           cur = cur.next;
       }
       return size;
    }
    //链表的属性  链表的头节点
    public ListNode head;//null
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }
}